package com.alibaba.citrus.cr.mn.order.center.facade.function.impl;

import com.alibaba.citrus.cr.common.enums.ordercenter.AsyncMessageEventEnum;
import com.alibaba.citrus.cr.common.enums.ordercenter.SalesOrderPushStatusEnum;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.AbstractOrderBizCloseService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.OrderAtomCloseService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.SendMsgService;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.mnordercenter.model.dto.CommonAsyncMessageResponse;
import com.epoch.app.mnordercenter.model.dto.OrderAtomBatchCloseRequest;
import com.epoch.app.mnordercenter.model.dto.PushOrderInfoToMnSideRequestOrderInfo;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCloseResponse;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangxiaorui
 * @ClassName SalesOrderBizCreateServiceImpl
 * @description: TODO
 * @date 2023/05/16 14:04
 * @version: 1.0
 */
@Service("salesOrderBizCloseService")
public class SalesOrderBizCloseServiceImpl extends AbstractOrderBizCloseService {
    private static final Log log = Log.getLogger(SalesOrderBizCloseServiceImpl.class);

    @Resource(name ="salesOrderAtomCloseService")
    private OrderAtomCloseService salesOrderAtomCloseService;
    @Resource
    private SendMsgService sendMsgService;

    @Override
    public Result<List<OrderAtomCloseResponse>> processAtomAbility(OrderAtomBatchCloseRequest request, List<OrderAtomCloseResponse> responses) {
        log.info("SalesOrderBizCloseServiceImpl#processAtomAbility responses={}", responses);
        try {
            if (CollectionUtils.isNotEmpty(request.getOrderList())) {
                List<OrderAtomCloseResponse> closeResponses = salesOrderAtomCloseService.batchClose(request);
                responses.addAll(closeResponses);
            }

            if (CollectionUtils.isNotEmpty(responses)) {
                // 发送消息通知来源系统
                List<PushOrderInfoToMnSideRequestOrderInfo> pushOrderInfoList = new ArrayList<>();
                for (OrderAtomCloseResponse closeResponse : responses) {
                    if (Boolean.TRUE.equals(closeResponse.getSuccess())) {
                        // 成功的不发kafka消息，要基于sap关闭回调结果发kafka消息
                        continue;
                    }
                    pushOrderInfoList.add(PushOrderInfoToMnSideRequestOrderInfo.builder()
                            .salesOrderNo(closeResponse.getSalesOrderNo())
                            .lineNum(closeResponse.getLineNum())
                            .orderStatus(SalesOrderPushStatusEnum.FAIL_REFUSED.getOrderStatusCode())
                            .message(closeResponse.getMessage())
                            .build());
                }

                // 发送kafka消息通知应用端处理结果
                CommonAsyncMessageResponse commonAsyncMessageResponse = new CommonAsyncMessageResponse();
                commonAsyncMessageResponse.setEventCode(AsyncMessageEventEnum.SALES_ORDER_REFUSED.getValue());
                commonAsyncMessageResponse.setEventName(AsyncMessageEventEnum.SALES_ORDER_REFUSED.getLabel());
                commonAsyncMessageResponse.setEventTime(String.valueOf(new Date().getTime()));
                commonAsyncMessageResponse.setFromSys(request.getFromSys());
                commonAsyncMessageResponse.setBusinessUnitCode(request.getBusinessUnitCode());
                commonAsyncMessageResponse.setData(pushOrderInfoList);
                sendMsgService.sendMsgToSendKafka(commonAsyncMessageResponse);
                log.info("SalesOrderBizCloseServiceImpl#processAtomAbility sendMsgToSendKafka message={}", commonAsyncMessageResponse);
            }
        } catch (Exception e) {
            log.error("SalesOrderBizCloseServiceImpl#processAtomAbility error", e);
            throw e;
        }
        return Result.success(Lists.newArrayList(), "", "");
    }
}
